#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e7+5,mod = 998244353;
const long double I = 1;
ll a[N],cnt[N];
int ty[N],n;
long double w[N][2];
bool check(double x)
{
    //cout<<x<<"\n";
    for(int i = 1;i <= n;i++) w[i][0] = w[i][1] = 0;
    for(int i = 1;i <= n;i++)
    {
        if(w[i][0] < 0) w[i][0] = 0;
        w[i][1] = 1-w[i][0]-ty[i]*x;
        w[i+1][0] = 2*x-w[i][1];
        //cout<<w[i][0]<<" "<<w[i][1]<<"\n";
        if(w[i][1] < 0) return 0;
    }
    return 1;
}
ll qpow(ll x,ll y)
{
    ll res = 1;
    for(;y;y>>=1,x=x*x%mod) if(y&1) res = res*x%mod;
    return res;
}
void solve()
{
    int nn,A,B,C,M;
    cin>>nn>>a[0]>>A>>B>>C>>M;
    for(int i = 1;i <= nn;i++)
    {
        a[i] = (a[i-1]*a[i-1]%M*A + a[i-1]*B + C)%M+1;
        cnt[a[i]]++;
        //cout<<a[i]<<"\n";
    }
    n = 0;
    for(int i = 1;i <= M;i++)
    {
        if(cnt[i] == 1) ty[++n] = 0;
        else if(cnt[i] > 1) ty[++n] = 1;
    }
    //cout<<n<<" "<<ty[1]<<" "<<ty[2]<<" "<<ty[3]<<" "<<ty[4]<<"\n";
    long double L = 0,R = 1,T = 100;
    while(T--)
    {
        double mid = (L+R)*0.5;
        if(check(mid)) L = mid;
        else R = mid;
    }
    long double mn = 1;
    ll p,q;
    for(int i = 1;i <= n*3;i++)
    {
        ll x = floor(R*i);
        //cout<<x<<" "<<i<<" "<<abs(I*x/i-L)<<"\n";
        //cout<<X*i%mod<<"\n";
        if(abs(I*x/i-L) < mn)
        {
            mn = abs(I*x/i-L);
            p = x,q = i;
        }
    }
    cout<<fixed<<setprecision(5)<<L<<" "<<p*qpow(q,mod-2)%mod<<"\n";
    for(int i = 1;i <= nn;i++) cnt[a[i]] = 0;
}
int main()
{
    //system("fc ex_guess2.ans guess.out");
    freopen("guess.in","r",stdin);
    freopen("guess.out","w",stdout);
    ios::sync_with_stdio(false);cin.tie(0);
    int T;cin>>T;
    while(T--) solve();
    return 0;
}